$(function(){
    var wfUrl = '/yiyuan/pintuan/start?rule=1';
    var swiper = new Swiper('.swiper-container', {
        slidesPerView: 'auto',
        grabCursor: true,
        autoplay: 2500,
        lazyLoading: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false,
    });
    $window=$(window);
    //设置$body的高度和宽度
    $body=$("body");
    $body.height($window.height());
    var BBL=BOBOLIB_fun().init($,this,$window,$body);
    var app=new MainApp(BBL);
    //弹出分享提示图片
    $('.shareBtn').click(function(){
        var strDiv=$('<div id="shareBtnImg"><img src="{__STATIC_URL__}/public/yiyuan/images/sharetip.png" alt="点击右上角【发送给朋友】或【分享到朋友圈】" width="90%"></div>');
        strDiv.css('text-align','right');
        var bimg=new BBL.BBase(strDiv,{dockType:9,resizeW:.9,resizeH:0.5,offsetX:0,offsetY:0,cssPosition:'fixed'});
        var lockDiv=app.popup(bimg);
        lockDiv=lockDiv.parent().find('#shareBtnImg').prev();
        lockDiv.width(lockDiv.width()+10);
        strDiv.click(function(){
            app.close();
        });
    });

    /**
     * 剩余时间倒计时正式版
     * 修改方案： 采用构造函数的形式，避免使用setInterval函数，
     *
     */
    var $lefttime=$('#countdownbtn').data('lefttime');
    var second = parseFloat($lefttime);
    if(!isNaN(second)){
        if(second<0) return;
        var $container = $('#countdownbtn');
        var url = $('#countdownbtn');
        var doInterval=function(h,m,s,ms,obj){//回调
            if(isNaN(h)){
                obj.stop();
                return;
            }
            $container.empty();//清空
            if(h<=9) h='0'+h;
            if(m<=9) m='0'+m;
            if(s<=9) s='0'+s;
            $container.html(h+':'+m+':'+s); //显示
            if(h==0&&m==0&&s==0) {
                obj.stop();
                app.popupWait(1);
                setTimeout(function() {
                    app.close();
                    location.reload(true);
                }, 4000);
                return;
            }
        }
        var cd=new BCountdown(second,doInterval);
        cd.start();//开始
    }
    /**
     * 计算开团人数以及金额
     */
    var obj = {};
    obj.calculate = function() {    
        this.data = {
            max_quan: null,
            unit_price:null,
            product_price: null
        };
        this.init = function ($input) {
            /**
             * quan 开团人数
             * product_price 商品价格
             * max_quan 最多的人数
             */
            var that = this;

            var max_quan = parseFloat($input.data('max'));
            var product_price = parseFloat($input.data('price'));
            this.data.product_price = product_price;
            this.data.max_quan = max_quan;

            $input.on('change', function () {
                var quan = parseFloat($(this).val());

                if (isNaN(quan) || quan <= 1 || typeof(quan) != 'number') {
                    quan = 2;
                    $(this).val(quan);
                }

                if (quan <= that.data.max_quan) {
                    var unit_price = that.data.product_price/quan;
                    unit_price = Math.round(unit_price * 100) / 100;
                    that.data.unit_price = unit_price;

                    $('.unit_price').html(that.data.unit_price);
                } else {
                        var unit_price = that.data.product_price / quan;
                        unit_price = Math.round(unit_price * 100) / 100;
                        that.data.unit_price = unit_price;

                        $info.find('.quan').val(that.data.max_quan);
                        $('.unit_price').html(that.data.unit_price);
                }
                that.para()
             })
        };
        this.para = function () {
            console.log('最多参与人数是：' + this.data.max_quan)
            console.log('每个人应该付钱是：' + this.data.unit_price)
            console.log('商品价格是：' + this.data.product_price)
        }
    }

    var cal = new obj.calculate();
    cal.init($('.quan'));

    // $('#tuwenxiangqing').on('click', function (e) {
    //     e.preventDefault();
    //     app.popupWebUrl('图文详情','/yiyuan/pintuan/start/get?id=<?php echo val($detail, 'id'); ?>&act=1',function(index){
    //     });
    // })
    $('#wanfajieshao').on('click', function (e) {
        var $wanfabox = $('#wanfa-container');
        e.preventDefault();
        var that = this;
        if( $(this).hasClass('showdetail')) {
            $wanfabox.empty();
            $(that).removeClass('showdetail')
        } else {
            $wanfabox.load(wfUrl, function (x) {    
                $(that).addClass('showdetail')
            });
        }
    })
});
